home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14148 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: FreeNet.Carleton.CA!aq436
  2. From: aq436@FreeNet.Carleton.CA (Jerry Boyd)
  3. Newsgroups: comp.lang.c
  4. Subject: Compare Function
  5. Date: 11 Apr 1996 23:46:07 GMT
  6. Organization: The National Capital FreeNet
  7. Sender: aq436@freenet2.carleton.ca (Jerry Boyd)
  8. Message-ID: <4kk5jv$41o@freenet-news.carleton.ca>
  9. Reply-To: aq436@FreeNet.Carleton.CA (Jerry Boyd)
  10. NNTP-Posting-Host: freenet2.carleton.ca
  11.  
  12.  
  13.  
  14.  
  15. How would I write a compare function (compare_strings, for 
  16. instance) that uses character pointers.  I need something 
  17. that would take a string and compare it to a list, or dictionary, 
  18. of other words.  If the word is in the dictionary, the 
  19. definition would be printed.  If the word is not in the dictionary,
  20. it would say something like, sorry, that word isn't in the dict.
  21.  
  22.  
  23. This is how the compare_strings function should look, but need to 
  24. implement using pointers.
  25.  
  26. int compare_strings (char s1[], char s2[])
  27. {
  28.   int i = 0, answer
  29.     
  30.   while ( s1[i] == s2[i] && s1[i]  != '\0' && s2[i] != '\0' )
  31.   ++i;
  32.  
  33.   if ( s1[i] < s2[i] )
  34.       answer = -1;
  35.   else if ( s1[i] == s2[i] )
  36.       answer = 0;
  37.   else
  38.       answer = 1;               /*  s1 > s2   */
  39.  
  40.   return (answer);
  41. }
  42.  
  43.  
  44.  
  45. The dictionary will look something like this, and will tie
  46. in with the rest of the program.
  47.  
  48. {{ "cobol",  "common busuness oriented language" },
  49.  { "fortran",  "formula translator" },
  50.  { "John Doe",  "God of programming" }} 
  51.  
  52.  
  53. --
  54.  
  55.